package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.*
import com.kiln.juxingqs.ui.viewmodel.MiddleVm

class ZrDetailModel(app: Application) : MiddleVm(app) {


    val goodsDetail = MutableLiveData<JsonResult<GoodsDetail>>()
    val tradeGoodsDetail = MutableLiveData<JsonResult<TradeGoodsDetail>>()

    val isShowing = MutableLiveData<Boolean>(false)
    val priceBuy = MutableLiveData<String>("")
    val buyNum = MutableLiveData<String>("")

    val priceSell = MutableLiveData<String>("")
    val sellNum = MutableLiveData<String>("")
    val guanzRet = MutableLiveData<JsonResult<Ret>>()
    val guanSellRet = MutableLiveData<JsonResult<Ret>>()
    val guanBuyRet = MutableLiveData<JsonResult<Ret>>()
    val callUser = MutableLiveData<JsonResult<UserInfo>>()

    //1.服务中心 2.摘牌供应商
    val user_status = MutableLiveData<Int>()

    val zhRet = MutableLiveData<JsonResult<Ret>>()
    val queRenRet = MutableLiveData<JsonResult<Ret>>()
    val buyRet = MutableLiveData<JsonResult<Ret>>()


    fun holdCall(id: String) {
        hold({
            server.trade_detail(id, "")
        }, {
        }, success = {
            goodsDetail.value = it
        }, complete = {
        })
    }

    fun holdGoodsDetailCall(id: String) {
        hold({
            server.trade_goods_detail(id)
        }, {
        }, success = {
            tradeGoodsDetail.value = it
        }, complete = {
        })
    }

    fun zhuanhuanPost(id: String, num: String) {
        hold({
            server.freeze_locking_transfer(id, num)
        }, {
            isShowing.value = true
        }, success = {
            zhRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun add_gz() {
        hold({
            server.gz_order(
                "${goodsDetail.value!!.data!!.goods_id!!}"
            )
        }, {
            isShowing.value = true
        }, success = {
            guanzRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun guaiMai() {
        if (priceBuy.value.isNullOrEmpty()) {
            toast("请输入挂买价格")
            return
        }
        if (buyNum.value.isNullOrEmpty()) {
            toast("请输入挂买数")
            return
        }

        if (priceBuy.value!!.toDouble() < goodsDetail.value!!.data!!.buy_start
            || priceBuy.value!!.toDouble() > goodsDetail.value!!.data!!.buy_end
        ) {

            toast("价格不能超出范围")
            return
        }
        hold({
            server.buyer_order(
                "${goodsDetail.value!!.data!!.goods_id!!}",
                "${buyNum.value!!}",
                "${priceBuy.value!!}",
            )
        }, {
            isShowing.value = true
        }, success = {
            guanBuyRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun guaiSell(b: GetBathRet.Bath) {
        if (priceSell.value.isNullOrEmpty()) {
            toast("请输入挂卖价格")
            return
        }
        if (sellNum.value.isNullOrEmpty()) {
            toast("请输入挂卖数")
            return
        }
        if (priceSell.value!!.toDouble() < goodsDetail.value!!.data!!.sell_start
            || priceSell.value!!.toDouble() > goodsDetail.value!!.data!!.sell_end
        ) {
            toast("价格不能超出范围")
            return
        }
        hold({
            server.sell_order(
                "${b.bath_id}",
                "${sellNum.value!!}",
                "${priceSell.value!!}",
            )
        }, {
            isShowing.value = true
        }, success = {
            guanSellRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun userInfo() {
        hold({
            server.userinfo().apply {
                body()?.let {
                    if (it.isSuccess) {
                        StoreUtil.saveBean(getApplication(), it.data!!)
                    }
                }
            }
        }, success = {
            callUser.value = it
        })
    }

    fun oneTouchRedeposit(goods_id: String) {
        hold({
            server.one_touch_redeposit(
                goods_id
            )
        }, {
            isShowing.value = true
        }, {
            queRenRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

    fun holdBuyYj(detail: TradeGoodsDetail, num: String) {
        hold({
            server.trade_goods_buy_yj(
                "${detail.goods_dict.goods_id}",
                "${detail.goods_dict.price}",
                num
            )
        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

    fun holdBuyTj(detail: TradeGoodsDetail, num: String) {
        hold({
            server.trade_goods_buy_tj(
                "${detail.goods_dict.goods_id}",
                "${detail.goods_dict.price_pf}",
                num
            )

        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

    fun holdBuyYh(detail: TradeGoodsDetail, num: String) {
        hold({
            server.trade_goods_buy_yh(
                "${detail.goods_dict.goods_id}",
                "${detail.goods_dict.price_pf}",
                num
            )

        }, {
            isShowing.value = true
        }, success = {
            buyRet.value = it
        }, complete = {
            isShowing.value = false

        })
    }

}